<template>
    <div class="background">
        <div class="clan-famous-council">
            <!-- 第一部分：标题和轮播图 -->
            <section class="hero-section">
                <h1 class="title">名人堂</h1>
                <p class="subtitle">传承家族文化，恪守名人德行</p>
                <div class="carousel-container">
                    <el-carousel :interval="4000" type="card" height="400px" arrow="always">
                        <el-carousel-item v-for="(person, index) in featuredPersons" :key="index"
                            @click="openDialog(person)">
                            <div class="carousel-card">
                                <div class="cover" :style="{ backgroundImage: `url(${person.cover})` }">
                                    <div class="cover-placeholder" v-if="!person.cover"></div>
                                </div>
                                <div class="name">{{ person.name }}</div>
                            </div>
                        </el-carousel-item>
                    </el-carousel>
                </div>
            </section>

            <!-- 第二部分：搜索和名人网格 -->
            <section class="search-section">
                <div class="search-header">
                    <el-input v-model="searchQuery" placeholder="搜索名人..." class="search-input" :prefix-icon="Search" />
                    <el-button type="text" class="more-button" @click="toMorePage">
                        更多 >
                    </el-button>
                </div>
                <div class="grid-container">
                    <div class="person-grid">
                        <div v-for="(person, index) in displayedPersons" :key="index" @click="openDialog(person)"
                            class="person-card" @mouseenter="hoverIndex = index" @mouseleave="hoverIndex = -1">
                            <div class="cover" :style="{
                                backgroundImage: person.cover ? `url(${person.cover})` : 'none',
                                transform: hoverIndex === index ? 'scale(1.05)' : 'scale(1)'
                            }">
                                <div class="cover-placeholder" v-if="!person.cover"></div>
                            </div>
                            <div class="info">
                                <div class="name">{{ person.name }}</div>
                                <div class="likes">
                                    <el-icon>
                                        <Star />
                                    </el-icon>
                                    {{ person.likes }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

            <FamousDetail v-if="showDialog" :person="selectedPerson" @close="showDialog = false" />
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { Search, Star } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import FamousDetail from './component/FamousDetail.vue'

const router = useRouter()
const showDialog = ref(false)
const selectedPerson = ref({
    id: 1,
    name: '张氏先祖',
    bio: '张氏家族创始人，生于明朝万历年间...',
    images: [],
    relationships: [],
    events: []
})

const openDialog = (person:any) => {
    selectedPerson.value = {
        ...person,
        bio: person.bio || '暂无简介',
        images: person.images || [],
        relationships: person.relationships || [],
        events: person.events || []
    }
    showDialog.value = true
}

// 模拟数据
const featuredPersons = [
    { name: '张氏先祖', cover: '' },
    { name: '李氏宗长', cover: '' },
    { name: '王氏贤达', cover: '' },
    { name: '赵氏名士', cover: '' },
    { name: '刘氏先贤', cover: '' }
]

const allPersons = [
    { name: '张氏先祖', cover: '', likes: 1245 },
    { name: '李氏宗长', cover: '', likes: 982 },
    { name: '王氏贤达', cover: '', likes: 756 },
    { name: '赵氏名士', cover: '', likes: 653 },
    { name: '刘氏先贤', cover: '', likes: 542 },
    { name: '陈氏先祖', cover: '', likes: 432 },
    { name: '杨氏宗长', cover: '', likes: 321 },
    { name: '黄氏贤达', cover: '', likes: 298 },
    { name: '周氏名士', cover: '', likes: 245 },
    { name: '吴氏先贤', cover: '', likes: 187 },
    { name: '郑氏先祖', cover: '', likes: 156 },
    { name: '孙氏宗长', cover: '', likes: 132 }
]

const searchQuery = ref('')
const hoverIndex = ref(-1)

const displayedPersons = computed(() => {
    return searchQuery.value
        ? allPersons.filter(person => person.name.includes(searchQuery.value))
        : allPersons.slice(0, 12)
})

const toMorePage = () => {
    router.push({ name: 'moreClanFamous' })
}
</script>

<style scoped lang="scss">
.background {
    background-color: #f5efe6;
}

.clan-famous-council {
    font-family: 'Microsoft YaHei', sans-serif;
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
    overflow-y: auto;
}

/* 第一部分样式 */
.hero-section {
    text-align: center;
    margin-bottom: 40px;

    .title {
        color: var(--el-color-primary);
        font-size: 36px;
        margin-bottom: 16px;
    }

    .subtitle {
        color: var(--el-color-success);
        font-size: 18px;
        margin-bottom: 30px;
    }

    .carousel-container {
        margin: 0 auto;
        max-width: 900px;

        .carousel-card {
            height: 100%;
            display: flex;
            flex-direction: column;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            cursor: pointer;

            .cover {
                flex: 1;
                background-size: cover;
                background-position: center;
                background-color: var(--el-color-primary-light-8);
                position: relative;

                .cover-placeholder {
                    position: absolute;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: var(--el-color-primary-light-8);
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    color: var(--el-color-primary);
                    font-size: 14px;
                }
            }

            .name {
                padding: 15px;
                background-color: var(--el-color-primary-light-9);
                color: var(--el-color-primary);
                font-size: 18px;
                font-weight: bold;
                text-align: center;
                border-top: 1px solid var(--el-border-color);
            }
        }
    }
}

/* 第二部分样式 */
.search-section {
    margin-bottom: 40px;

    .search-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .search-input {
            width: 300px;
        }

        .more-button {
            color: var(--el-color-primary);
            font-size: 14px;
        }
    }

    .person-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 20px;

        .person-card {
            background: #fff;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            cursor: pointer;

            &:hover {
                box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            }

            .cover {
                height: 180px;
                background-size: cover;
                background-position: center;
                transition: transform 0.3s ease;
                background-color: var(--el-color-primary-light-8);
                position: relative;

                .cover-placeholder {
                    position: absolute;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    background-color: var(--el-color-primary-light-8);
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    color: var(--el-color-primary);
                    font-size: 14px;
                }
            }

            .info {
                padding: 15px;
                position: relative;
                border-top: 1px solid var(--el-border-color);

                .name {
                    color: var(--el-color-primary);
                    font-size: 16px;
                    margin-bottom: 8px;
                }

                .likes {
                    position: absolute;
                    right: 15px;
                    bottom: 15px;
                    color: var(--el-color-warning);
                    font-size: 14px;
                    display: flex;
                    align-items: center;

                    .el-icon {
                        margin-right: 4px;
                    }
                }
            }
        }
    }
}

/* 响应式调整 */
@media (max-width: 992px) {
    .person-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

@media (max-width: 768px) {
    .person-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

@media (max-width: 576px) {
    .person-grid {
        grid-template-columns: 1fr !important;
    }
}
</style>